function [ roulette_individuals ] = roulette( individuals, n_of_selection )
% ROULETTE
%   Este metodo recibe una serie de individuos codificados como vectores y
%   devuelve un subconjunto de n_of_selection cantidad de acuerdo al
%   algoritmo de ruleta

fitness_array = zeros(1, length(individuals));
f_array = zeros(1, length(individuals));
for i = 1:length(individuals)
   f_array(i) = fitness(individuals{i});
end
for i = 1:length(individuals)
   fitness_array(i) = f_array(i) + 2*abs(min(f_array));
end

total_fitness = sum(fitness_array);

relative_fitness = zeros(1, length(individuals));
accumulative_fitness = zeros(1, length(individuals));
for i = 1:length(individuals)
    relative_fitness(i) = fitness_array(i) / total_fitness;
    if i > 1
        accumulative_fitness(i) = accumulative_fitness(i-1) + relative_fitness(i);
    else
        accumulative_fitness(i) = relative_fitness(i);
    end
end

r = rand(1, n_of_selection);
roulette_individuals = cell(1, n_of_selection);

for i = 1:n_of_selection
    j = 1;
    while r(i) > accumulative_fitness(j)
        j = j + 1;
    end
    roulette_individuals{i} = individuals{j};
end

end

